using System;
using System.Windows.Forms;

namespace demo
{
    /// <summary>
    /// MessagePushClient使用示例
    /// 演示如何使用MessagePushClient类进行消息推送客户端操作
    /// </summary>
    public partial class MessagePushClientUsageExample : Form
    {
        private MessagePushClient messagePushClient;
        private ListBox lstMessages;
        private StatusStrip statusStrip;
        private ToolStripStatusLabel statusLabel;
        private Button btnConnect;
        private Button btnDisconnect;
        private TextBox txtServerIP;
        private TextBox txtServerPort;
        private Button btnSendChat;
        private Button btnSendBroadcast;
        private Button btnSendHeartbeat;
        private TextBox txtMessage;
        private TextBox txtTargetUser;

        public MessagePushClientUsageExample()
        {
            InitializeComponent();
            InitializeMessagePushClient();
        }

        private void InitializeComponent()
        {
            // 初始化UI控件
            this.lstMessages = new ListBox();
            this.statusStrip = new StatusStrip();
            this.statusLabel = new ToolStripStatusLabel();
            this.btnConnect = new Button();
            this.btnDisconnect = new Button();
            this.txtServerIP = new TextBox();
            this.txtServerPort = new TextBox();
            this.btnSendChat = new Button();
            this.btnSendBroadcast = new Button();
            this.btnSendHeartbeat = new Button();
            this.txtMessage = new TextBox();
            this.txtTargetUser = new TextBox();

            // 设置控件属性
            this.lstMessages.Location = new System.Drawing.Point(12, 80);
            this.lstMessages.Size = new System.Drawing.Size(560, 250);

            this.btnConnect.Text = "连接服务器";
            this.btnConnect.Location = new System.Drawing.Point(12, 12);
            this.btnConnect.Size = new System.Drawing.Size(100, 30);
            this.btnConnect.Click += BtnConnect_Click;

            this.btnDisconnect.Text = "断开连接";
            this.btnDisconnect.Location = new System.Drawing.Point(120, 12);
            this.btnDisconnect.Size = new System.Drawing.Size(100, 30);
            this.btnDisconnect.Click += BtnDisconnect_Click;

            this.txtServerIP.Text = "127.0.0.1";
            this.txtServerIP.Location = new System.Drawing.Point(230, 12);
            this.txtServerIP.Size = new System.Drawing.Size(100, 20);

            this.txtServerPort.Text = "8080";
            this.txtServerPort.Location = new System.Drawing.Point(340, 12);
            this.txtServerPort.Size = new System.Drawing.Size(60, 20);

            this.txtMessage.Text = "测试消息";
            this.txtMessage.Location = new System.Drawing.Point(12, 50);
            this.txtMessage.Size = new System.Drawing.Size(200, 20);

            this.txtTargetUser.Text = "目标用户";
            this.txtTargetUser.Location = new System.Drawing.Point(220, 50);
            this.txtTargetUser.Size = new System.Drawing.Size(100, 20);

            this.btnSendChat.Text = "发送聊天";
            this.btnSendChat.Location = new System.Drawing.Point(330, 48);
            this.btnSendChat.Size = new System.Drawing.Size(80, 25);
            this.btnSendChat.Click += BtnSendChat_Click;

            this.btnSendBroadcast.Text = "发送广播";
            this.btnSendBroadcast.Location = new System.Drawing.Point(420, 48);
            this.btnSendBroadcast.Size = new System.Drawing.Size(80, 25);
            this.btnSendBroadcast.Click += BtnSendBroadcast_Click;

            this.btnSendHeartbeat.Text = "心跳";
            this.btnSendHeartbeat.Location = new System.Drawing.Point(510, 48);
            this.btnSendHeartbeat.Size = new System.Drawing.Size(60, 25);
            this.btnSendHeartbeat.Click += BtnSendHeartbeat_Click;

            this.statusStrip.Items.Add(this.statusLabel);
            this.statusLabel.Text = "未连接";

            // 添加控件到窗体
            this.Controls.Add(this.lstMessages);
            this.Controls.Add(this.btnConnect);
            this.Controls.Add(this.btnDisconnect);
            this.Controls.Add(this.txtServerIP);
            this.Controls.Add(this.txtServerPort);
            this.Controls.Add(this.txtMessage);
            this.Controls.Add(this.txtTargetUser);
            this.Controls.Add(this.btnSendChat);
            this.Controls.Add(this.btnSendBroadcast);
            this.Controls.Add(this.btnSendHeartbeat);
            this.Controls.Add(this.statusStrip);

            // 设置窗体属性
            this.Text = "MessagePushClient 使用示例";
            this.Size = new System.Drawing.Size(600, 420);
        }

        private void InitializeMessagePushClient()
        {
            // 注释掉网络消息相关代码
            /*
            // 创建MessagePushClient实例
            messagePushClient = new MessagePushClient(lstMessages, statusLabel, statusStrip);

            // 订阅事件
            messagePushClient.ConnectionStatusChanged += OnConnectionStatusChanged;
            messagePushClient.MessageReceived += OnMessageReceived;
            messagePushClient.ErrorOccurred += OnErrorOccurred;
            */
        }

        private void BtnConnect_Click(object sender, EventArgs e)
        {
            // 注释掉网络连接相关代码
            /*
            try
            {
                string serverIP = txtServerIP.Text.Trim();
                int serverPort = int.Parse(txtServerPort.Text.Trim());

                // 连接到服务器
                var result = messagePushClient.ConnectToServer(serverIP, serverPort, "TestUser");
                
                if (result.Success)
                {
                    MessageBox.Show("连接成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show($"连接失败：{result.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"连接时发生错误：{ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            */
            MessageBox.Show("网络连接功能已禁用", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void BtnDisconnect_Click(object sender, EventArgs e)
        {
            // 注释掉断开连接相关代码
            /*
            try
            {
                // 断开连接
                messagePushClient.Disconnect();
                MessageBox.Show("已断开连接", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                MessageBox.Show($"断开连接时发生错误：{ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            */
            MessageBox.Show("网络连接功能已禁用", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void BtnSendChat_Click(object sender, EventArgs e)
        {
            // 注释掉发送聊天消息相关代码
            /*
            try
            {
                string message = txtMessage.Text.Trim();
                string targetUser = txtTargetUser.Text.Trim();
                
                if (string.IsNullOrEmpty(message))
                {
                    MessageBox.Show("请输入消息内容", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }

                // 发送聊天消息
                bool isPrivate = !string.IsNullOrEmpty(targetUser) && targetUser != "目标用户";
                messagePushClient.SendChatMessage(message, isPrivate, isPrivate ? targetUser : null);
                
                MessageBox.Show($"消息已发送: {message}", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                MessageBox.Show($"发送消息时发生错误：{ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            */
            MessageBox.Show("消息发送功能已禁用", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void BtnSendBroadcast_Click(object sender, EventArgs e)
        {
            // 注释掉发送广播消息相关代码
            /*
            try
            {
                string message = txtMessage.Text.Trim();
                
                if (string.IsNullOrEmpty(message))
                {
                    MessageBox.Show("请输入消息内容", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }

                // 发送广播消息
                var broadcastMessage = new BroadcastMessage
                {
                    Text = message,
                    Sender = "TestUser"
                };
                messagePushClient.SendMessage(broadcastMessage);
                
                MessageBox.Show($"广播消息已发送: {message}", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                MessageBox.Show($"发送广播消息时发生错误：{ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            */
            MessageBox.Show("广播消息功能已禁用", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void BtnSendHeartbeat_Click(object sender, EventArgs e)
        {
            // 注释掉发送心跳消息相关代码
            /*
            try
            {
                // 发送心跳消息
                messagePushClient.SendHeartbeat();
                MessageBox.Show("心跳消息已发送", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                MessageBox.Show($"发送心跳消息时发生错误：{ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            */
            MessageBox.Show("心跳消息功能已禁用", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        // 连接状态变化事件处理 - 已注释
        /*
        private void OnConnectionStatusChanged(object sender, ConnectionStatusEventArgs e)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new Action(() => OnConnectionStatusChanged(sender, e)));
                return;
            }

            btnConnect.Enabled = !e.IsConnected;
            btnDisconnect.Enabled = e.IsConnected;
            
            string status = e.IsConnected ? "已连接" : "未连接";
            this.Text = $"MessagePushClient 使用示例 - {status}";
        }

        // 消息接收事件处理
        private void OnMessageReceived(MessageModel message)
        {
            // MessagePushClient已经处理了UI更新，这里可以添加额外的处理逻辑
            Console.WriteLine($"收到消息：{message.Content} (类型：{message.Type}, 发送者：{message.Sender})");
            
            // 根据消息类型进行不同的处理
            switch (message.Type)
            {
                case MessageType.Chat:
                    // 处理聊天消息
                    break;
                case MessageType.Broadcast:
                    // 处理广播消息
                    break;
                case MessageType.Error:
                    // 处理错误消息
                    break;
                case MessageType.LoginResponse:
                    // 处理登录响应
                    break;
                default:
                    // 处理其他类型消息
                    break;
            }
        }

        // 错误发生事件处理
        private void OnErrorOccurred(object sender, ErrorEventArgs e)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new Action(() => OnErrorOccurred(sender, e)));
                return;
            }

            MessageBox.Show($"发生错误：{e.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        */

        protected override void OnFormClosing(FormClosingEventArgs e)
        {
            // 窗体关闭时断开连接 - 已注释
            /*
            messagePushClient?.Disconnect();
            */
            base.OnFormClosing(e);
        }
    }

    /// <summary>
    /// 简单的使用示例 - 在现有窗体中集成MessagePushClient（已注释）
    /// </summary>
    public class SimpleUsageExample
    {
        public static void IntegrateIntoExistingForm(Form form, ListBox messageListBox, ToolStripStatusLabel statusLabel, StatusStrip statusStrip)
        {
            // 网络消息相关代码已注释
            /*
            // 创建MessagePushClient实例
            var messagePushClient = new MessagePushClient(messageListBox, statusLabel, statusStrip);

            // 订阅事件（可选）
            messagePushClient.ConnectionStatusChanged += (sender, e) => {
                // 处理连接状态变化
                Console.WriteLine($"连接状态变化：{(e.IsConnected ? "已连接" : "未连接")}");
            };

            messagePushClient.MessageReceived += (message) => {
                // 处理消息接收
                Console.WriteLine($"收到消息：{message.Content} (类型：{message.Type})");
            };

            messagePushClient.ErrorOccurred += (sender, e) => {
                // 处理错误
                MessageBox.Show($"错误：{e.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            };

            // 连接到服务器
            var result = messagePushClient.ConnectToServer("127.0.0.1", 8080, "TestUser");
            if (!result.Success)
            {
                MessageBox.Show($"连接失败：{result.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            // 在窗体关闭时断开连接
            form.FormClosing += (sender, e) => {
                messagePushClient.Disconnect();
            };
            */
            MessageBox.Show("网络消息功能已禁用", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
    }
}